package cn.murky.admin.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.murky.admin.auth.domain.dto.LoginDto;
import cn.murky.admin.auth.service.IMurkyLoginService;
import cn.murky.admin.auth.utils.CaptchaUtil;
import cn.murky.security.entity.SecurityUserInfo;
import cn.murky.admin.system.api.SysMenuApi;
import cn.murky.admin.system.api.domian.SysMenuTree;
import cn.murky.admin.system.api.enums.MenuType;
import cn.murky.core.web.ApiResult;
import cn.murky.admin.core.utils.SecurityUtils;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.Solon;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.Valid;

import java.util.Arrays;
import java.util.List;

import static com.anji.captcha.controller.CaptchaController.getRemoteId;

/***
 * 安全控制器
 *
 * @auth hans
 */
@Controller
@Valid
@Mapping("auth")
public class MurkyAuthController {

    @Inject
    private IMurkyLoginService iMurkyLoginService;
    @Inject
    private SysMenuApi sysMenuApi;
    @Inject
    private CaptchaService captchaService;

//    @Post
//    @Mapping("/login")
//    public ResponseModel get(CaptchaVO captchaVO) {
//        //必传参数：captchaVO.captchaVerification
//        ResponseModel response = captchaService.verification(captchaVO);
//        if (response.isSuccess() == false) {
//            //验证码校验失败，返回信息告诉前端
//            //repCode  0000  无异常，代表成功
//            //repCode  9999  服务器内部异常
//            //repCode  0011  参数不能为空
//            //repCode  6110  验证码已失效，请重新获取
//            //repCode  6111  验证失败
//            //repCode  6112  获取验证码失败,请联系管理员
//            //repCode  6113  底图未初始化成功，请检查路径
//            //repCode  6201  get接口请求次数超限，请稍后再试!
//            //repCode  6206  无效请求，请重新获取验证码
//            //repCode  6202  接口验证失败数过多，请稍后再试
//            //repCode  6204  check接口请求次数超限，请稍后再试!
//        }
//        return response;
//    }


    @Post
    @Mapping("/captcha")
    public ApiResult<Object> check(CaptchaVO data, Context request) {
        data.setBrowserInfo(getRemoteId(request));
        ResponseModel responseModel = this.captchaService.check(data);
        return CaptchaUtil.parseApiResult(responseModel);
    }

    /**
     * 获取图形验证码信息
     * @return token
     */
    @Get
    @Mapping("/captcha")
    public ApiResult<Object> getCaptcha(CaptchaVO data, Context request) {
        assert request.realIp() != null;

        data.setBrowserInfo(getRemoteId(request));

        ResponseModel responseModel = captchaService.get(data);
        return CaptchaUtil.parseApiResult(responseModel);
    }

    /**
     * 登录
     * @param loginDto loginDto
     * @return token
     */
    @Post
    @Mapping("login")
    public ApiResult<String> login(@Body LoginDto loginDto) {
//        ResponseModel response = captchaService.verification(captchaVO);

        SaTokenInfo tokenInfo = iMurkyLoginService.login(loginDto);
        return ApiResult.ok(tokenInfo.getTokenValue());
    }

    /**
     * 登出
     */
    @Post
    @Mapping("logout")
    public ApiResult<?> logout() {
//        SecurityUtils.delUserMenu();
        SecurityUtils.delUserInfo();
        StpUtil.logout();
        return ApiResult.ok();
    }

    /**
     * 获取菜单
     * @return List<SysMenuTree>
     */
    @Get
    @ApiOperation("获取菜单")
    @Mapping("menu")
    public ApiResult<List<SysMenuTree>> menu() {
        return ApiResult.ok(sysMenuApi.treeSysMenu(Arrays.asList(MenuType.MENU, MenuType.DIRECTORY)));
    }

    /**
     * 获取用户信息
     * @return SecurityUserInfo
     */
    @Get
    @Mapping("info")
    public ApiResult<SecurityUserInfo> userInfo() {
        return ApiResult.ok(iMurkyLoginService.userInfo());
    }
}
